package com.hope.aimouse;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

public class CommonUtils {

     public static void launchApp(String packageName, Context context) {
        try {
            // 获取包管理器
            PackageManager pm = context.getPackageManager();
            // 获取启动意图
            Intent launchIntent = pm.getLaunchIntentForPackage(packageName);

            if (launchIntent != null) {
                // 添加标志，确保在新的任务栈中启动
                launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // 启动应用
                context.startActivity(launchIntent);
            } else {
                // 如果找不到应用，可以跳转到应用商店
                try {
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                    marketIntent.setData(Uri.parse("market://details?id=" + packageName));
                    marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(marketIntent);
                } catch (Exception e) {
                    // 如果没有应用商店，可以跳转到浏览器的应用商店页面
                    Intent webIntent = new Intent(Intent.ACTION_VIEW);
                    webIntent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
                    webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(webIntent);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "无法启动应用", Toast.LENGTH_SHORT).show();
        }
    }

    public static void takeScreenshot(Context context) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.TAKE_SCREENSHOT");
        context.sendBroadcast(intent);
    }
}
